<?php
namespace app\admin\controller;
use app\admin\model\ColList as ColListModel;
use QL\QueryList;

class ColList extends Base
{
    public function test3(){
        echo 1;
    }
    public function lst()
    {   	
//  	$link = new LinkModel();
//  	$query = '';
//  	if(request()->isGet()){
//          $param = $this->request->param();
//          if (isset($param['keywords'])){
//              $query = $param['keywords'];
//          }
//      }        
//      if($query != ''){
//          $links = $links->where('title', 'like', '%' . $query . '%')->order('id desc')->paginate(15,false,[
//          	'query' => request()->param(),
//          ]);
//      }else{
//      	$links = $links->order('id desc')->paginate(15,false,[]);
//      }
//      $this->assign('links',$links);
//      $this->assign('keywords', $query);  
//  	return view();
    	
    	
    	
    	
        $link = new LinkModel();
        if(request()->isPost()){
            $sorts = input('post.');
            foreach ($sorts as $k => $v) {
                $link->update(['id'=>$k,'sort'=>$v]);
            }
            $this->success('更新排序成功！',url('lst'));
            return;
        }
        $links = $link->order('sort asc')->paginate(5);
        $this->assign('links',$links);
        return view();
    }
    public function add()
    {
        if(request()->isPost()){
            $data = input('post.');
            $validate = \think\Loader::validate('Link');
            if(!$validate->scene('add')->check($data)){
                $this->error($validate->getError());
                die;
            }
            if(db('Link')->insert($data)){
                $this->success('添加链接成功','lst');
            }else{
                $this->error('添加链接失败');
            }
            return;
        }
        return view();
    }

    public function edit(){
        $id = input('id');
        $link = new LinkModel();
        $list = $link->find($id);
        if (request()->isPost()){
            $data = [
                'id'=>$id,
                'title'=>input('title'),
                'url'=>input('url'),
                'desc'=>input('desc'),
            ];
            $validate = \think\Loader::validate('Link');
            if(!$validate->scene('edit')->check($data)){
                $this->error($validate->getError());
                die;
            }
            $save = $link->update($data);
            if($save !== false){
                $this->success('修改链接成功','lst');
            }else{
                $this->error('修改链接失败');
            }
            return;
        }
        $this->assign('list', $list);
        return view();
    }

    public function del(){
        $id = input('id');
        if(db('Link')->delete($id)){
            $this->success("删除链接成功！", 'lst');
        }else{
            $this->error('删除链接失败');
        }
    }

    public function test(){
//        $data = QueryList::Query('http://cms.querylist.cc/bizhi/453.html',['image' => ['img','src']])->data;
//        $data = QueryList::Query('https://mp.weixin.qq.com/s/b1juLqWbPMwrZfIF0FJkaQ',['t'=>['.js_inner','html']])->data;
        $data = QueryList::Query('https://mp.weixin.qq.com/s/b1juLqWbPMwrZfIF0FJkaQ',['t'=>['iframe','html']])->data;
        //打印结果
        print_r($data);
    }

    public function test2(){
//        $data = QueryList::Query('http://cms.querylist.cc/bizhi/453.html',['image' => ['img','src']])->data;
//        $data = QueryList::Query('https://mp.weixin.qq.com/s/b1juLqWbPMwrZfIF0FJkaQ',['t'=>['.js_inner','html']])->data;
        $data = QueryList::Query('https://www.itjc8.com/forum-36-1.html',[
            'title'=>['#lf_1 dd a','text'],
            'url'=>['#lf_1 dd a','href']
        ])->data;
        $colListModel = new ColListModel();
        $add = $colListModel->saveAll($data);
//        return json($data);
        //打印结果
//        print_r($data);
    }

    public function throughCol(){
        $colListArr = ColListModel::all()->toArray();
//        return json($colListArr);die;
//        print_r($colListArr);die;
        foreach ($colListArr as $item){
//            echo $item['url'];die;
            $url = $item['url'];
            $arr1 = QueryList::Query($url, [
                'title'=>['#threadlisttableid tbody[id^=normalthread_] th .xst','text'],
                'url'=>['#threadlisttableid tbody[id^=normalthread_] th .xst','href']
            ])->data;
            print_r($arr1);die;
        }
//        return json($colListArr);
    }
}
